package com.ruoyi.system.utils;

import com.ruoyi.system.domain.BydspecialPoject;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class ExcelDownUtils {

    public static ResponseEntity<byte[]> downloadExcel(List<BydspecialPoject> bydspecialPojects) throws IOException {
        // 1. 准备数据
        // 2. 创建Excel工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("用户数据");

        // 3. 创建表头
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("部门");
        headerRow.createCell(1).setCellValue("业务员");
        headerRow.createCell(2).setCellValue("客户名称");
        headerRow.createCell(3).setCellValue("客户订单号");
        headerRow.createCell(4).setCellValue("类别");
        headerRow.createCell(5).setCellValue("物料代码");
        headerRow.createCell(6).setCellValue("产品名称");
        headerRow.createCell(7).setCellValue("产品型号");
        headerRow.createCell(8).setCellValue("合同备注");
        headerRow.createCell(9).setCellValue("单位");
        headerRow.createCell(10).setCellValue("订单数量");
        headerRow.createCell(11).setCellValue("下单日期");
        headerRow.createCell(12).setCellValue("合同交期");
        headerRow.createCell(13).setCellValue("变更后交期");
        headerRow.createCell(14).setCellValue("责任工程师");
        headerRow.createCell(15).setCellValue("在制状态");
        headerRow.createCell(16).setCellValue("入库时间");
        headerRow.createCell(17).setCellValue("出货日期");
        headerRow.createCell(18).setCellValue("是否外协");
        headerRow.createCell(19).setCellValue("投产数量");
        headerRow.createCell(20).setCellValue("已发货数量");

        // 4. 填充数据
        for (int i = 0; i < bydspecialPojects.size(); i++) {

            Row dataRow = sheet.createRow(i + 1);
            String out = null;
            BydspecialPoject bydspecialPoject = bydspecialPojects.get(i);
            if (bydspecialPoject.getOutsourced() != null && bydspecialPoject.getOutsourced() != ""){
                if (bydspecialPoject.getOutsourced().equals("0")){
                    out = "是";
                }
            }
            else {
                 out = "否";
            }
            dataRow.createCell(0).setCellValue(bydspecialPoject.getDepartment());
            dataRow.createCell(1).setCellValue(bydspecialPoject.getSalesman());
            dataRow.createCell(2).setCellValue(bydspecialPoject.getCustomerName());
            dataRow.createCell(3).setCellValue(bydspecialPoject.getCustomerOrder());
            dataRow.createCell(4).setCellValue(bydspecialPoject.getCategory());
            dataRow.createCell(5).setCellValue(bydspecialPoject.getMaterialCode());
            dataRow.createCell(6).setCellValue(bydspecialPoject.getProductName());
            dataRow.createCell(7).setCellValue(bydspecialPoject.getProductModel());
            dataRow.createCell(8).setCellValue(bydspecialPoject.getContractRemarks());
            dataRow.createCell(9).setCellValue(bydspecialPoject.getUnit());
            dataRow.createCell(10).setCellValue(bydspecialPoject.getOrderQuantity());
            dataRow.createCell(11).setCellValue(bydspecialPoject.getOrderDate());
            dataRow.createCell(12).setCellValue(bydspecialPoject.getDeliveryDate());
            dataRow.createCell(13).setCellValue(bydspecialPoject.getChangeDate());
            dataRow.createCell(14).setCellValue(bydspecialPoject.getResponsibleEngineer());
            dataRow.createCell(15).setCellValue(bydspecialPoject.getProductionStatus());
            dataRow.createCell(16).setCellValue(bydspecialPoject.getStorageTime());
            dataRow.createCell(17).setCellValue(bydspecialPoject.getShippingDate());
            dataRow.createCell(18).setCellValue(out);
            dataRow.createCell(19).setCellValue(bydspecialPoject.getProductionQuantity());
            dataRow.createCell(20).setCellValue(bydspecialPoject.getQuantityShipped());
        }

        // 5. 自适应列宽
        for (int i = 0; i <21; i++) {
            sheet.autoSizeColumn(i);
        }

        // 6. 将工作簿写入字节流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        byte[] excelBytes = outputStream.toByteArray();
        workbook.close();

        // 7. 设置响应头
        HttpHeaders headers = new HttpHeaders();
        // 设置正确的Excel文件MIME类型
        headers.setContentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));

        // 正确编码中文文件名，支持不同浏览器
        String fileName = "订单数据.xlsx";
        String encodedFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");

        // 设置Content-Disposition，兼容不同浏览器
        headers.setContentDispositionFormData("attachment", encodedFileName);
        headers.add("Content-Disposition", "attachment;filename*=UTF-8''" + encodedFileName);

        // 8. 返回Excel文件
        return ResponseEntity.ok()
                .headers(headers)
                .body(excelBytes);
    }
}
